<?php
/**
 * CEOfox
 *
 * @copyright  Copyright 2011-2012 CEOfox, Inc.
 * @license    http://ceofox.com/pages/license
 * @version    $Id: following.class.php 97084 2013-05-24 09:40:17 $
 * @author     CEOfox, Inc.
 */
?>
<?php
defined('PHPFOX') or exit('NO DICE!');

class Follow_Component_Block_Following extends Phpfox_Component
{ 
    public function process()
    {
        $iPage = $this->getParam('page', 1);
        $iOffset = ($iPage - 1) * phpfox::getParam('follow.showeachtime');
        $bCanFollow = false;
        $aUser = $this->getParam('aUser');
        $sType = $this->getParam('type_id', 'user');
        $bIsCurrentUser = false;
        $bIsLogin = ( Phpfox::getUserId() > 0 )? true: false;
        if(!isset($aUser) || ($aUser['user_id'] == phpfox::getUserId()))
        {
            $aUser['user_id'] = phpfox::getUserId();
            $bIsCurrentUser = true;
        }
        if($aUser['user_id'] != phpfox::getUserId())
        {
            if(Phpfox::getService('follow')->canFollow($aUser['user_id']))
            {
                $bCanFollow = true;
            }
        }
        list($iTotalFollowing, $aFollowings) = phpfox::getService('follow')->getFollowing($aUser['user_id'],$iOffset,Phpfox::getParam('follow.showeachtime'), $sType);
        $aResult = array();  
        foreach($aFollowings as $aFollowing)
        {
            $aTemp = phpfox::getservice('user')->get($aFollowing['item_id']);
            if($aFollowing['user_id'] == $aUser['user_id'] && $sType == 'user')
            {
                $aTemp['canFollow'] = 0;
                $aTemp['isFollow'] = 0;
            }
            else
            {
                if(Phpfox::getService('follow')->canFollow($aFollowing['user_id'], $sType))
                {
                    if(!phpfox::getService('follow')->isFollow(Phpfox::getUserId(),$aFollowing['user_id'], $sType))
                    {
                        $aTemp['isFollow'] = 0;
                    }
                    else
                    {
                        $aTemp['isFollow'] = 1;
                    }
                    $aTemp['canFollow'] = 1;
                }
                else
                {   
                    $aTemp['isFollow'] = 0;
                    $aTemp['canFollow'] = 0;
                }
            }  
            $aResult[] = $aTemp;
        }
        $this->template()->assign(array(
            'bIsCurrentUser' => $bIsCurrentUser,
            'iTotalFollowing' => $iTotalFollowing,
            'iCurrentFollowing' => isset($aFollowings)?count($aFollowings):0,
            'sFullControllerName'  => Phpfox::getLib('module')->getFullControllerName(),
            'aFollowings' => $aResult,
            'sType' => $sType,
            'iPage' => $iPage,
            'sBlockType' => 'following'
        ));
        
    }
    public function clean()
    {        
        (($sPlugin = Phpfox_Plugin::get('follow.component_block_following_clean')) ? eval($sPlugin) : false);
    }
}

?>
